package com.example.pathtracker.activity

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.example.pathtracker.R
import com.github.mikephil.charting.charts.PieChart
import com.github.mikephil.charting.charts.BarChart
import com.github.mikephil.charting.data.*
import com.github.mikephil.charting.utils.ColorTemplate
import com.github.mikephil.charting.formatter.IndexAxisValueFormatter

class DataStatsActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_data_stats)
        supportActionBar?.title = "数据统计"
        supportActionBar?.setDisplayHomeAsUpEnabled(true)

        setupPathTypePieChart()
        setupPointTypePieChart()
        setupBarChart()
    }

    private fun setupPathTypePieChart() {
        val pieChart = findViewById<PieChart>(R.id.pieChartPath)
        val entries = listOf(
            PieEntry(15f, "人行道"),
            PieEntry(10f, "机动车道"),
            PieEntry(7f, "消防车道"),
            PieEntry(5f, "摩托车道"),
            PieEntry(3f, "其他")
        )
        val dataSet = PieDataSet(entries, "路径类型分布")
        dataSet.colors = ColorTemplate.MATERIAL_COLORS.toList()
        val data = PieData(dataSet)
        data.setValueTextSize(14f)
        pieChart.data = data
        pieChart.description.isEnabled = false
        pieChart.centerText = "路径类型"
        pieChart.animateY(1000)
        pieChart.invalidate()
    }

    private fun setupPointTypePieChart() {
        val pieChart = findViewById<PieChart>(R.id.pieChartPoint)
        val entries = listOf(
            PieEntry(12f, "消防栓"),
            PieEntry(6f, "无人机"),
            PieEntry(8f, "灭火器"),
            PieEntry(4f, "消防站"),
            PieEntry(5f, "卫生间"),
            PieEntry(2f, "其他")
        )
        val dataSet = PieDataSet(entries, "点位类型分布")
        dataSet.colors = ColorTemplate.COLORFUL_COLORS.toList()
        val data = PieData(dataSet)
        data.setValueTextSize(14f)
        pieChart.data = data
        pieChart.description.isEnabled = false
        pieChart.centerText = "点位类型"
        pieChart.animateY(1000)
        pieChart.invalidate()
    }

    private fun setupBarChart() {
        val barChart = findViewById<BarChart>(R.id.barChart)
        val entries = listOf(
            BarEntry(0f, 20f),
            BarEntry(1f, 15f),
            BarEntry(2f, 10f),
            BarEntry(3f, 5f),
            BarEntry(4f, 8f),
            BarEntry(5f, 12f),
            BarEntry(6f, 18f)
        )
        val dataSet = BarDataSet(entries, "近一周采集量")
        dataSet.colors = ColorTemplate.LIBERTY_COLORS.toList()
        val data = BarData(dataSet)
        data.setValueTextSize(14f)
        barChart.data = data
        barChart.xAxis.valueFormatter = IndexAxisValueFormatter(
            listOf("周一", "周二", "周三", "周四", "周五", "周六", "周日")
        )
        barChart.xAxis.granularity = 1f
        barChart.xAxis.setDrawGridLines(false)
        barChart.axisLeft.axisMinimum = 0f
        barChart.axisRight.isEnabled = false
        barChart.description.isEnabled = false
        barChart.animateY(1000)
        barChart.invalidate()
    }

    override fun onOptionsItemSelected(item: android.view.MenuItem): Boolean {
        if (item.itemId == android.R.id.home) {
            onBackPressedDispatcher.onBackPressed()
            return true
        }
        return super.onOptionsItemSelected(item)
    }
}
